package com.beijing.springbootclustersession.controller;

import com.beijing.springbootclustersession.mapper.UserService;
import com.beijing.springbootclustersession.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**登录页面处理
 * @Authther: mrwu
 * @date:2021/4/1-04
 * @Description:com.beijing.springbootclustersession.controller
 */
@Controller
public class LoginCtroller {

    @Autowired
    UserService userService;
    @RequestMapping("/login/index")
    public String loginstatic(){

        return "login";
    }



    @PostMapping("/login")
    public String login(User user, HttpSession session, Model model){
        System.out.println(user);
        //如果用户存在就写入session
        User result = userService.findUser(user);
        if (user !=null){
            session.setAttribute("user", user.getName());

            model.addAttribute("user", result);
            //跳转到成功页面
            return "redirect:/sucess";
        }

     return "login/index";
    }

}
